<?php
declare (strict_types = 1);

namespace app\api\dao;

use app\api\model\Cate;
use think\facade\Db;

//题目
class QuestionDao
{
    public $questionListModel;
    public $model;

    //题库分类
    public function cate($class)
    {
        return DB::table("zl_questions_cate")
            ->where("calss",$class)
            ->where("is_show",10)
            ->select();
    }

    //章节分类
    public function chapter($class,$cate,$label)
    {
         $this->model =DB::table("zl_questions_chapter")
            ->field("id,name,sort,ids")
            ->where("cate","_".$cate."_")
            ->where("is_show",10)
            ->where('label', '=', trim($label))
            ->order("sort","asc");

         $chapters = $this->model->select()->toArray();

         foreach ($chapters as &$chapter)
         {
             $chapter["count"]=count(json_decode($chapter["ids"]));
         }



        return $chapters;
    }

    //标签分类
    public function label($class,$cate,$label)
    {
        $this->model =DB::table("zl_questions_label")
            ->field("id,name,sort,ids")
            ->where("cate","_".$cate."_")
            ->where("class",$class)
            ->where('label', '=', trim($label))
            ->order("sort","asc");

        $labels = $this->model->select()->toArray();

        foreach ($labels as &$labelV)
        {
            $labelV["count"]=count(json_decode($labelV["ids"]));
        }

        return $labels;
    }

    //图片组分类
    public function group($class)
    {
        $this->model =DB::table("zl_questions_image_grop")
            ->field("id,group_name,count")
            ->where("class",$class);

        $groups = $this->model->select()->toArray();
        foreach ($groups as &$group)
        {
            $group["images"]=DB::table("zl_questions_image")->field("title,content,image_url")->where("group_id",$group["id"])->limit(5)->select();
        }

        return $groups;
    }

    //图片组分类
    public function images($class,$group_id)
    {
        $this->model =DB::table("zl_questions_image")
            ->field("title,content,image_url")
            ->where("group_id",$group_id)
            ->where("class",$class);

        $images = $this->model->select()->toArray();

        return $images;
    }

    //所有题目ID
    public  function all($where,$class): array
    {
        $this->questionListModel= DB::table("zl_questions_content")
            ->where("class",$class)
            ->when(isset($where["city"]) && $where["city"],function ($query) use($where){
                $query->where(function ($query) use($where){
                    $query->where("label3",$where["city"])->whereOr('label3','');
                });
            })
            ->when(!isset($where["city"]) || !$where["city"],function ($query) use($where){
                $query->where('label3','');
            })
            ->field("id");
        if(!isset($where["is_vip"]))  $where["is_vip"]=10;
        // 检索查询条件
        !empty($where) && $this->setWhere($where);

        //返回结果
        return $this->questionListModel->select()->toArray();
    }


    //题目列表
    public  function list($where,$vip,$class): array
    {
        $this->questionListModel= DB::table("zl_questions_content")
            ->field("id,question,key_words,answer,media_url,media_type,is_vip,item1,item2,item3,item4,item5,item6,item7,item8,concise_explains,explains,type,type_c,label,label1,label2,label3,difficulty,wrong_rate")
            ->where("class",$class)
            ->where("is_vip",$vip)
            ->where("id","in",$where);

        //返回结果
        return $this->questionListModel->select()->toArray();
    }


    /**
     * 设置查询条件
     * @param $where
     */
    public function setWhere($where)
    {
        if (isset($where['label']) && !empty($where['label'])) {

            $this->questionListModel->where('label', '=', trim($where['label']));
        }

//        if (isset($where['city']) && !empty($where['city'])) {
//
//            $this->questionListModel->where('label', '=', trim($where['label']));
//        }

        if (isset($where['label1']) && !empty($where['label1'])) {

            $this->questionListModel->where('label1', '=', trim($where['label1']));
        }

        if (isset($where['label2']) && !empty($where['label2'])) {

            $this->questionListModel->where('label2', '=', trim($where['label2']));
        }

        if (isset($where['label3']) && !empty($where['label3'])) {

            $this->questionListModel->where('label3', '=', trim($where['label3']));
        }

        if (isset($where['is_vip']) && !empty($where['is_vip'])) {

            $this->questionListModel->where('is_vip', '=', $where['is_vip']);
        }

        if (isset($where['cate']) && !empty($where['cate'])) {

            $cates=explode("_",$where['cate']);
            if($cates)
            {
                foreach ($cates as $cate)
                {
                    $this->questionListModel->where('cate', 'like', '%' . "_".trim($cate)."_" . '%');
                }
            }
        }




    }



}
